package com.pengpeng.server.logic.responsemessage;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.pengpeng.server.dao.ResponseHistoryMessageDao;
import com.pengpeng.server.dao.ResponseRealMessageDao;
import com.pengpeng.server.entity.PengPengMessage;
import com.pengpeng.server.entity.User;
import com.pengpeng.server.entity.UserRelationship;


public class ResponseMessageFactory { 
	
     private ResponseRealMessageDao responserealmessagedao;
     private ResponseHistoryMessageDao responsehistorymessagedao;
	 private Map<String,Map<String,PengPengMessage>> usersreponsemessagemap=null;
     
     private ResponseMessageFactory()
     {
    	 usersreponsemessagemap=new HashMap<String,Map<String,PengPengMessage>>();
    	 usersreponsemessagemap=Collections.synchronizedMap(usersreponsemessagemap);
     }
	public Map<String,PengPengMessage> getUserreponsemessages(String userphonenum) {
		return usersreponsemessagemap.get(userphonenum);
	}//getUserreponsemessages
	
	
	public void setUserreponsemessage(String userphonenum,PengPengMessage responsemessage)
	{    
		  if(true==usersreponsemessagemap.containsKey(userphonenum))
		  {   
			  Map<String,PengPengMessage> userresponsemap;
			  userresponsemap=usersreponsemessagemap.get(userphonenum);
			  if(null==userresponsemap)
			  {
				 userresponsemap=new HashMap<String,PengPengMessage>();
				 userresponsemap=Collections.synchronizedMap(userresponsemap);
				 usersreponsemessagemap.put(userphonenum, userresponsemap);
			  }
			  userresponsemap.put(responsemessage.getMessageid(), responsemessage);
		  }
		
	      responserealmessagedao.insert(responsemessage);
	}//setUserreponsemessage
	
	
    public boolean removeUserAllMessages(String userphonenum)
    {
    	usersreponsemessagemap.remove(userphonenum);
    	responsehistorymessagedao.copyaFromRealResponseMessages(userphonenum);
    	responserealmessagedao.deleteByUserphonenum(userphonenum);
    	return true;
    }//removeUserAllMessages
    
    
    public boolean removeUserMessage(String userphonenum,PengPengMessage responsemessage)
    {
    	if(true==usersreponsemessagemap.containsKey(userphonenum))
		  {   
			  Map<String,PengPengMessage> userresponsemap;
			  userresponsemap=usersreponsemessagemap.get(userphonenum);
			  if(null!=userresponsemap)
			  {
				  userresponsemap.remove(responsemessage.getMessageid());
			  }
			  
		  }
    	
    	responsehistorymessagedao.insert(responsemessage);
    	responserealmessagedao.deleteByKey(userphonenum, responsemessage.getMessageid());
    	return true;
    }//removeUserMessage
    
    
	public ResponseRealMessageDao getResponserealmessagedao() {
		return responserealmessagedao;
	}//getResponserealmessagedao
	
	public void setResponserealmessagedao(
			ResponseRealMessageDao responserealmessagedao) {
		this.responserealmessagedao = responserealmessagedao;
	}//setResponserealmessagedao
	
	
	public ResponseHistoryMessageDao getResponsehistorymessagedao() {
		return responsehistorymessagedao;
	}//getResponsehistorymessagedao
	
	
	public void setResponsehistorymessagedao(
			ResponseHistoryMessageDao responsehistorymessagedao) {
		this.responsehistorymessagedao = responsehistorymessagedao;
	}//setResponsehistorymessagedao
	
	
	public boolean loadUserResponseMessage(User user)
	{  
		String userphonenum=user.getPhonenum();
		if(false==usersreponsemessagemap.containsKey(userphonenum))
		{
			Map<String,PengPengMessage> responsemessagemap;
			responsemessagemap=responserealmessagedao.queryByUserphonenum(userphonenum);
			usersreponsemessagemap.put(userphonenum, responsemessagemap);
		}
		
		return true;
	}//loadUserResponseMessage
}
